package grow_task

import (
	"go_server/game_server/api/service/bag"
	"go_server/game_server/constants"
	"go_server/game_server/dao/task_info_dao"
	"go_server/game_server/dataconfig/config_manager"
	"go_server/game_server/dataconfig/configpb"
	"go_server/game_server/protocol/pb"
	"go_server/game_server/util"
	"go_server/game_server/world_manager"
	"go_server/zinx/ziface"
	"go_server/zinx/znet"
	"gorm.io/datatypes"
)

type GetGrowTaskPrizeApi struct {
	znet.BaseRouter
}

// 领取任务奖励
func (*GetGrowTaskPrizeApi) Handle(request ziface.IRequest) {
	msg := &pb.GetGrowTaskPrizeC2S{}
	if !util.PBParseRet(request, msg) {
		return
	}

	conn := request.GetConnection()
	roleId := ServiceObj.GetRoleId(request)

	taskConfig, ok := config_manager.GetStcTableTask_SheetTaskById(msg.TaskId)
	if !ok {
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_GROW_BRANCH_TASK_UNFIND, nil)
		return
	}

	if taskConfig.Type == constants.GROW_TASK {
		ServiceObj.GetGrowTaskPrize.gotGrowPrizeHandle(request, conn, roleId, msg, &taskConfig)
	} else {
		ServiceObj.GetGrowTaskPrize.gotBranchPrizeHandle(request, conn, roleId, msg, &taskConfig)
	}

	// 推送成长、支线任务列表下行
	msgS2C := ServiceObj.GetGrowTaskList.GetGrowTaskListApiS2C(roleId)
	conn.SendBuffPBMsg(uint32(pb.ProtocolManager_GetDailyTaskList), msgS2C)

	// 发送奖励
	bag.AddItemArrayInt32(roleId, taskConfig.TaskReward)
}

func (*GetGrowTaskPrizeApi) gotGrowPrizeHandle(request ziface.IRequest, conn ziface.IConnection, roleId int64, msg *pb.GetGrowTaskPrizeC2S, taskConfig *configpb.StcTableTask_SheetTask_27074BFE) {
	fields := []string{"grow_task_id", "grow_task"}          // 需要查询或者修改的字段
	row := task_info_dao.GetTaskInfoByRoleId(roleId, fields) // 获取当前用户任务进度

	var growTaskId int32                        // 当日活跃度
	growTaskProgress := make(map[int32][]int32) // 成长任务进度

	if row != nil {
		growTaskId = row.GrowTaskId

		if row.GrowTask.Data() != nil {
			growTaskProgress = row.GrowTask.Data()
		}
	}

	if growTaskId <= 0 {
		growTaskId = firstGrowTaskId
	}

	if growTaskId != msg.TaskId {
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_GROW_BRANCH_TASK_UNFIND, nil)
		return
	}

	// 根据任务ID从玩家任务进度中获取任务信息
	growTaskInfo, exist := growTaskProgress[msg.TaskId]
	if !exist || growTaskInfo[2] != 1 {
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_TASK_UNCOMPLETE, nil)
		return
	}

	growTaskProgress[msg.TaskId][2] = 2      // 任务状态改为已领取
	growTaskId = taskConfig.PrereQuisites[0] // 进入下一个成长任务

	res := &pb.GetGrowTaskPrizeS2C{
		TaskId: msg.TaskId,
		State:  growTaskProgress[msg.TaskId][2],
	}

	// 优先返回下行
	conn.SendBuffPBMsg(uint32(pb.ProtocolManager_GetGrowTaskPrize), res)

	// 修改任务状态
	taskInfo := task_info_dao.TaskInfo{
		RoleId:     roleId,
		GrowTaskId: growTaskId,
		GrowTask:   datatypes.NewJSONType(growTaskProgress),
	}
	task_info_dao.UpdateTaskInfo(&taskInfo, fields)
}

func (*GetGrowTaskPrizeApi) gotBranchPrizeHandle(request ziface.IRequest, conn ziface.IConnection, roleId int64, msg *pb.GetGrowTaskPrizeC2S, taskConfig *configpb.StcTableTask_SheetTask_27074BFE) {
	fields := []string{"branch_task"}
	row := task_info_dao.GetTaskInfoByRoleId(roleId, fields) // 获取当前用户任务进度

	branchTaskProgress := make(map[int32][]int32) // 支线任务进度

	if row != nil && row.BranchTask.Data() != nil {
		branchTaskProgress = row.BranchTask.Data()
	}

	// 根据任务ID从玩家任务进度中获取任务信息
	branchTaskInfo, exist := branchTaskProgress[msg.TaskId]
	if !exist || branchTaskInfo[2] != 1 {
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_TASK_UNCOMPLETE, nil)
		return
	}

	branchTaskProgress[msg.TaskId][2] = 2 // 任务状态改为已领取

	res := &pb.GetGrowTaskPrizeS2C{
		TaskId: msg.TaskId,
		State:  branchTaskProgress[msg.TaskId][2],
	}

	// 优先返回下行
	conn.SendBuffPBMsg(uint32(pb.ProtocolManager_GetGrowTaskPrize), res)

	// 修改任务状态
	taskInfo := task_info_dao.TaskInfo{
		RoleId:   roleId,
		GrowTask: datatypes.NewJSONType(branchTaskProgress),
	}
	task_info_dao.UpdateTaskInfo(&taskInfo, fields)
}
